﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.BL;
using System.Data;
using MandalaySoft.Common;

public partial class School_CourseManager : System.Web.UI.Page
{
    MyError _error = new MyError();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session[Global.Session_SchoolID] != null)
            {
                BindSubject();
                _error = new MyError();
                General.BindGradeLevelList(ddlCourseGradeLevel, Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), string.Empty, true, out _error);
                ddlCourseGradeLevel.Items.Insert(0, new ListItem("Select", "-1"));
            }
            else
            {
                Response.Redirect("~/default.aspx");
            }
        }
    }

    protected void gvSubject_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearSubject();
        int _subjectID = Convert.ToInt32(gvSubject.SelectedRow.Cells[0].Text);
        ShowSubjectForm();
        ShowSubject(_subjectID);
        BindCourse(_subjectID);
    }

    protected void gvCourse_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearCourse();
        int _courseID = Convert.ToInt32(gvCourse.SelectedRow.Cells[0].Text);
        ShowCourseForm();
        ShowCourse(_courseID);
        BindCoursePeriod(_courseID);
    }

    protected void gvCoursePeriod_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearCoursePeriod();
        int _coursePeriodID = Convert.ToInt32(gvCoursePeriod.SelectedRow.Cells[0].Text);
        hidCoursePeriodID.Value = _coursePeriodID.ToString();
        lblMessage.Text = "Course Period ID : " + _coursePeriodID.ToString() + " was selected.";
        ShowCoursePeriod(_coursePeriodID);
        ShowCoursePeriodForm();
    }

    private void BindSubject()
    {
        try
        {
            SubjectDB _db = new SubjectDB();
            DataTable _dt = new DataTable("tblSubjectList");
            _error = new MyError();
            _dt = _db.GetSubjectList(Convert.ToInt32(Session[Global.Session_SchoolID].ToString()), out _error);
            gvSubject.DataSource = _dt;
            gvSubject.DataBind();
            if (_error.ErrorNo != 0)
            {
                lblMessage.Text = _error.ErrorMessage;
            }

        }
        catch (Exception Ex)
        {
            lblMessage.Text = "Error at BindSubject(): " + Ex.Message;
        }
    }

    private void BindCourse(int SubjectID)
    {
        try
        {
            CourseDB _db = new CourseDB();
            DataTable _dt = new DataTable("tblCourseList");
            _error = new MyError();
            _dt = _db.GetCourseList(SubjectID, out _error);
            gvCourse.DataSource = _dt;
            gvCourse.DataBind();
            if (_error.ErrorNo != 0)
            {
                lblMessage.Text = _error.ErrorMessage;
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = "Error at BindCourse(): " + Ex.Message;
        }
    }


    private void BindCoursePeriod(int CourseID)
    {
        try
        {
            CoursePeriodDB _db = new CoursePeriodDB();
            DataTable _dt = new DataTable("tblCourseList");
            _error = new MyError();
            _dt = _db.GetCoursePeriodList(CourseID, out _error);
            gvCoursePeriod.DataSource = _dt;
            gvCoursePeriod.DataBind();
            if (_error.ErrorNo != 0)
            {
                lblMessage.Text = _error.ErrorMessage;
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = "Error at BindCoursePeriod(): " + Ex.Message;
        }
    }



    protected void btnSubjectSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("Subject");
        if (Page.IsValid)
        {
            Subject _item = new Subject();
            SubjectDB _db = new SubjectDB();
            _item.SubjectID = Convert.ToInt32(hidSubjectID.Value);
            _item.SchoolID = Convert.ToInt32(Session[Global.Session_SchoolID].ToString());
            _item.SubjectName = txtSubjectName.Text;
            _item.ShortName = txtSubjectShortName.Text;
            _item.IsActive = true;
            _item.CreatedBy = -1;
            _item.ModifiedBy = -1;

            _item = _db.Save(_item);
            hidSubjectID.Value = _item.SubjectID.ToString();
            lblMessage.Text = _item.Error.ErrorMessage;
            if (_item.Error.ErrorNo == 0)
            {
                BindSubject();
            }
        }
    }

    private void ShowSubjectForm()
    {
        tbcSubject.Visible = true;
        tbcCourse.Visible = false;
        tbcCoursePeriod.Visible = false;
        divCourseList.Visible = true;
        gvCourse.SelectedIndex = -1;
        divCoursePeriod.Visible = false;
    }

    private void ShowSubject(int SubjectID)
    {
        Subject _item = new Subject();
        SubjectDB _db = new SubjectDB();

        _item = _db.GetItem(SubjectID);
        if (_item.SubjectID > 0)
        {
            hidSubjectID.Value = _item.SubjectID.ToString();
            txtSubjectName.Text = _item.SubjectName;
            txtSubjectShortName.Text = _item.ShortName;
        }

        lblMessage.Text = _item.Error.ErrorMessage;
    }

    private void ClearSubject()
    {
        hidSubjectID.Value = "-1";
        txtSubjectName.Text = string.Empty;
        txtSubjectShortName.Text = string.Empty;
    }

    protected void btnNewSubject_Click(object sender, EventArgs e)
    {
        ClearSubject();
        ShowSubjectForm();
    }


    private void ShowCourseForm()
    {
        tbcSubject.Visible = false;
        tbcCourse.Visible = true;
        tbcCoursePeriod.Visible = false;
        gvCoursePeriod.SelectedIndex = -1;
        divCoursePeriod.Visible = true;
    }

    private void ShowCourse(int CourseID)
    {
        Course _item = new Course();
        CourseDB _db = new CourseDB();

        _item = _db.GetItem(CourseID);
        if (_item.CourseID > 0)
        {
            hidCourseID.Value = _item.CourseID.ToString();
            txtCourseName.Text = _item.CourseName;
            txtCourseShortName.Text = _item.ShortName;
            ddlCourseGradeLevel.Text = _item.GradeLevelID.ToString();
        }

        lblMessage.Text = _item.Error.ErrorMessage;
    }

    private void ClearCourse()
    {
        hidCourseID.Value = "-1";
        txtCourseName.Text = string.Empty;
        txtCourseShortName.Text = string.Empty;

        if (ddlCourseGradeLevel.Items.Count > 0)
        {
            ddlCourseGradeLevel.SelectedIndex = 0;
        }
    }

    protected void btnNewCourse_Click(object sender, EventArgs e)
    {
        ClearCourse();
        ShowCourseForm();
    }

    private void ShowCoursePeriodForm()
    {
        tbcSubject.Visible = false;
        tbcCourse.Visible = false;
        tbcCoursePeriod.Visible = true;
    }

    protected void btnNewCoursePeriod_Click(object sender, EventArgs e)
    {
        ClearCoursePeriod();
        ShowCoursePeriodForm();
    }

    private void ClearCoursePeriod()
    {
        hidCoursePeriodID.Value = "-1";
        txtCoursePeriodName.Text = string.Empty;
        txtCoursePeriodShortName.Text = string.Empty;
        ddlCoursePeriodTeacher.ClearSelection();
        ddlCoursePeriodAssTeacher.ClearSelection();
        ddlCoursePeriodPeriod.ClearSelection();
        ddlCoursePeriodGradeScale.ClearSelection();
        ddlCoursePeriodGenderRestriction.ClearSelection();
        ddlCoursePeriodCalendar.ClearSelection();
        ddlCoursePeriodParent.ClearSelection();
        txtCoursePeriodSeatNo.Text = string.Empty;
        txtCoursePeriodCredit.Text = string.Empty;
        chkDays.ClearSelection();
        chkTakeAttendence.Checked = false;
        chkAffectClassRank.Checked = false;
        chkAffectHonorRoll.Checked = false;
        chkCourseWeighted.Checked = false;
        chkHalfDay.Checked = false;
        chkAllowTeacherGradeScale.Checked = false;
    }

    private void ShowCoursePeriod(int CoursePeriodID)
    {
        CoursePeriod _item = new CoursePeriod();
        CoursePeriodDB _db = new CoursePeriodDB();

        _item = _db.GetItem(CoursePeriodID);
        if (_item.CoursePeriodID > 0)
        {
            hidCoursePeriodID.Value = _item.CoursePeriodID.ToString();
            txtCoursePeriodName.Text = _item.PeriodName;
            txtCoursePeriodShortName.Text = _item.ShortName;
            ddlCoursePeriodTeacher.Text = _item.TeacherID.ToString();
            ddlCoursePeriodAssTeacher.Text = _item.AssTeacherID.ToString();
            ddlCoursePeriodRoom.Text = _item.RoomID.ToString();
            ddlCoursePeriodPeriod.Text = _item.PeriodID.ToString();
            ddlCoursePeriodGradeScale.Text = _item.GradeScaleID.ToString();
            ddlCoursePeriodGenderRestriction.Text = _item.GenderRestriction.ToString();
            ddlCoursePeriodCalendar.Text = _item.CalendarID.ToString();
            ddlCoursePeriodParent.Text = _item.ParentCoursePeriodID.ToString();
            txtCoursePeriodSeatNo.Text = _item.SeatNo.ToString();
            txtCoursePeriodCredit.Text = _item.Credit.ToString();
            string dayList = _item.DayList;
            chkDays.ClearSelection();
            foreach (char dayChar in dayList.ToCharArray())
            {
                chkDays.Items.FindByValue(dayChar.ToString()).Selected = true;
            }

            chkTakeAttendence.Checked = _item.IsTakeAttendence;
            chkAffectClassRank.Checked = _item.IsAffectClassRank;
            chkAffectHonorRoll.Checked = _item.IsAffectHonorRoll;
            chkCourseWeighted.Checked = _item.IsCourseWeighted;
            chkHalfDay.Checked = _item.IsHalfDay;
            chkAllowTeacherGradeScale.Checked = _item.IsAllowTeacherGradeScale;
        }

        lblMessage.Text = _item.Error.ErrorMessage;
    }

    protected void btnCoursePeriodSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("CoursePeriod");
        if (Page.IsValid)
        {
            CoursePeriod _item = new CoursePeriod();
            CoursePeriodDB _db = new CoursePeriodDB();

            _item.CoursePeriodID = Convert.ToInt32(hidCoursePeriodID.Value);
            _item.CourseID = Convert.ToInt32(hidCourseID.Value);
            _item.PeriodName = txtCoursePeriodName.Text;
            _item.ShortName = txtCoursePeriodShortName.Text;
            _item.TeacherID = Convert.ToInt32(ddlCoursePeriodTeacher.Text);
            _item.AssTeacherID = Convert.ToInt32(ddlCoursePeriodAssTeacher.Text);
            _item.RoomID = Convert.ToInt32(ddlCoursePeriodRoom.Text);
            _item.PeriodID = Convert.ToInt32(ddlCoursePeriodPeriod.Text);
            _item.GradeScaleID = Convert.ToInt16(ddlCoursePeriodGradeScale.Text);
            _item.GenderRestriction = Convert.ToByte(ddlCoursePeriodGenderRestriction.Text);
            _item.CalendarID = Convert.ToInt32(ddlCoursePeriodCalendar.Text);
            _item.ParentCoursePeriodID = Convert.ToInt32(ddlCoursePeriodParent.Text);
            _item.SeatNo = Convert.ToInt16(txtCoursePeriodSeatNo.Text);
            _item.Credit = Convert.ToDecimal(txtCoursePeriodCredit.Text);
            string dayList = string.Empty;

            foreach (ListItem chk in chkDays.Items)
            {
                if (chk.Selected)
                {
                    dayList += chk.Value;
                }
            }

            _item.DayList = dayList;
            _item.IsTakeAttendence = chkTakeAttendence.Checked;
            _item.IsAffectHonorRoll = chkAffectHonorRoll.Checked;
            _item.IsAffectClassRank = chkAffectClassRank.Checked;
            _item.IsHalfDay = chkHalfDay.Checked;
            _item.IsAllowTeacherGradeScale = chkAllowTeacherGradeScale.Checked;
            _item.IsCourseWeighted = chkCourseWeighted.Checked;
            _item.IsActive = true;
            _item.CreatedBy = -1;
            _item.ModifiedBy = -1;

            _item = _db.Save(_item);
            hidCoursePeriodID.Value = _item.CoursePeriodID.ToString();
            lblMessage.Text = _item.Error.ErrorMessage;
            if (_item.Error.ErrorNo == 0)
            {
                BindCoursePeriod(Convert.ToInt32(hidCourseID.Value));
            }
        }
    }

    protected void btnCourseSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("Course");
        if (Page.IsValid)
        {
            Course _item = new Course();
            CourseDB _db = new CourseDB();

            _item.CourseID = Convert.ToInt32(hidCourseID.Value);
            _item.SubjectID = Convert.ToInt32(hidSubjectID.Value);
            _item.CourseName = txtCourseName.Text;
            _item.ShortName = txtCourseShortName.Text;
            _item.GradeLevelID = Convert.ToInt32(ddlCourseGradeLevel.Text);
            _item.IsActive = true;
            _item.CreatedBy = -1;
            _item.ModifiedBy = -1;

            _item = _db.Save(_item);
            hidCourseID.Value = _item.CourseID.ToString();
            lblMessage.Text = _item.Error.ErrorMessage;
            if (_item.Error.ErrorNo == 0)
            {
                BindCourse(Convert.ToInt32(hidSubjectID.Value));
            }
        }
    }
}